package com.swufe.lianxi;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyList extends AppCompatActivity implements AdapterView.OnItemLongClickListener{
    private static final String TAG = "ratelist2";
    Handler handler;
    ArrayList<HashMap<String, String>> listItems;
    private SimpleAdapter listItemAdapter;
    private ArrayAdapter adapter;
    private GridView list;
    private ProgressBar bar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list);//自定义布局
        View view= this.getLayoutInflater().inflate((R.layout.activity_my_list2), null);
        //ListView listView=(ListView) findViewById(R.id.mylist);
        list =view.findViewById(R.id.mylistView);
        bar=findViewById(R.id.progressBar);
        //listView.setVisibility(View.INVISIBLE);

        //list.setEmptyView(bar);

        String list_data[]={"one","two","three","four"};
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_activated_1,list_data);
        list.setAdapter(adapter);
        list.setOnItemLongClickListener(this);


        handler = new Handler(Looper.myLooper()) {
            //收到数据
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 5) {
                    List<String> list = (List<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String>(MyList.this,
                            android.R.layout.simple_list_item_1,
                            list);
                    //MyAdapter myAdapter = new MyAdapter(RatelistActivity2.this, R.layout.list_item, list);
                    //listView.setAdapter(listItemAdapter);
                    MyList.this.list.setAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };
       MyTask myTask=new MyTask();
       myTask.setHandler(handler);
       Thread t=new Thread(myTask);
       t.start();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("请确定是否删除当前数据");
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Log.i(TAG, "onClick: 对话框事件处理");
                adapter.remove(i);
                //adapter.remove(position);
                adapter.notifyDataSetChanged();
            }
        }).setNegativeButton("否",null);
        builder.create().show();
        return true;
    }
}